
local ClientOHOH = _G.ClientOHOH;
local M = ClientOHOH.class.new(script, ClientOHOH.files.Client_PlayerComponent(),script.Parent.Name);
local UIManager = ClientOHOH.load.UIManager();

local DailyReward_Cfg = ClientOHOH.config.DailyReward
local FunctionList = ClientOHOH.load.FunctionList();
FunctionList.sing_in=true
local LanguageHelper=_G.LanguageHelper --1:购买，2装备，3卸载
local GlobalTipGui =UIManager.ui_list.GlobalTipGui;
local DaySinGui =UIManager:CreateUI("DaySinGui",require(script.DaySinGui))
local ItemTool=_G.CommonOHOH.load.ItemTool();

local ConnectClass=_G.CommonOHOH.load.Connect();

local CallBackManager = ClientOHOH.load.CallBackManager();
local InstanceManager = ClientOHOH.load.InstanceManager();

local DAY_TIME=24*60*60
local MAX_DAY=ClientOHOH.config.Game.SignIn_Count


function M.new(self,...)
	return M(self,...);
end;

function M.InitDailyReward(self, lv)

	for k,v in pairs(DailyReward_Cfg) do
		v["award"]={}
		v["DataChange"]=ConnectClass.new()
		v["ChangeByLevel"]=function(this,level)
			this.award={}
			for j,p in pairs(this.item_id) do
				local a,b= ItemTool.GetRewardByLevel(j,p,level)
				this.award[a]=b
			end
		
            this.DataChange:Action()
        end
		v:ChangeByLevel(lv)
	end
	return DailyReward_Cfg

end
function M.ctor(self, parent)
	M.super.ctor(self, parent);
	
	self.DataBase=self.parent:GetComponent(ClientOHOH.load.Client_PlayerDataComponent());
    self.Level=self.DataBase:GetData("base").level


	local dataBase=self.parent:GetComponent(ClientOHOH.load.Client_PlayerDataComponent());
	self.data = dataBase:GetData("sing_in");

	local signeId,CannotSig=self:GetSingInId()

	self.singInId=signeId

	self.DailyReward=self:InitDailyReward(self.Level)

    DaySinGui:Init(self.DailyReward,self.singInId,CannotSig,self.data.id)
	UIManager:PushShow("DaySinGui")
	print("show")

	if CannotSig then
		
	else
		--DaySinGui:Show()s
	end


    DaySinGui.ClaimAction:Connect(function()
        if  self.parent:Invoke(FunctionList.sing_in) then
            GlobalTipGui:Show(LanguageHelper:GetText(4))
			-- HomeGui:HideCanSigIn()
			self.parent.SignInEvent:Action()
			DaySinGui:Hide()
            return true
        end

    end)


	self.parent.UserDataEvent:Connect(function(data)
        if data["base"] and data["base"].level~=self.Level then
            self.Level=data["base"].level
            if self.DailyReward  then
                for _,v in pairs(self.DailyReward) do
                    v:ChangeByLevel(self.Level)
                end
            end
        end
    end)


	local Singin=ClientOHOH:GetPath_Loop("Singin",game.Workspace.Touch)
	Singin.ChildAdded:Connect(function(child)
		local touchPart = child
		CallBackManager:AddRbxConnect(self,touchPart.Touched, function(obj,chPart)
			if not _G.CommonOHOH.load.GeneralTool().IsMeTouchEvent(chPart,"LeftLowerLeg") then
				return 
			end
			DaySinGui:Show()
	
		end);
	
		CallBackManager:AddRbxConnect(self,touchPart.TouchEnded, function(obj,chPart)
			if not _G.CommonOHOH.load.GeneralTool().IsMeTouchEvent(chPart,"LeftLowerLeg") then
				return 
			end
			 DaySinGui:Hide()
	
		end);
	
	end)



end;

function M.dtor(self)
	M.super.dtor(self);
end;

--获取签到ID，非连续
function M.GetSingInId(self)

	local day_s,end_s=self:GetDayStartAndEnd(self.data.time)

	if os.time()>end_s then
		local id=self.data.id+1
		if id>MAX_DAY then
			id=1
		end
		return id
	else

		return self.data.id,true --不能够领取
	end

end

--获取连续签到ID，如果超过一天没签到，重新从第一天签到
function M.GetSingInIdContinuity(self)
	--print("---GetSingInId---:",self.data.time)
	--print("----GetSingInId------0")
	if self.data.time==0 then --初次进入游戏，发放第一天奖励
		--print("----GetSingInId------1")
		return 1
	end
	

	local day_s,end_s=self:GetDayStartAndEnd(self.data.time)
	--签到时间已经超过了一天（有中断，发放第一天的奖励)
	if os.time()>end_s then

		local id=self.data.id+1
		if id>MAX_DAY then
			id=1
		end
		return id
	end

	if os.time()>day_s and os.time()<end_s then
		local id=self.data.id+1
		if id>MAX_DAY then
			id=1
		end
		return id
	end

	return self.data.id,true --不能够领取

end;

function M.GetDayStartAndEnd(self,time)
	local day=math.floor(time/DAY_TIME)*DAY_TIME
	return day,day+DAY_TIME
end

return M